﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.gameState.malaria.component;
using Imaginecup.observer;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using imaginecup.util;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.collision;
using Imaginecup.util;
using Imaginecup.stage;
using Imaginecup.gameState.malaria.component.scene;
using Imaginecup.loop;
using Imaginecup.scene;
using Imaginecup.gameState.malaria.stage;
using Imaginecup.componentSystem.component;
using Imaginecup.component.scene;

namespace Imaginecup.gameState.malaria.manager
{
    public class SceneMan : ISceneInfo, ISceneMan
    {
        private List<ICmpScene> m_SceneList = new List<ICmpScene>();
        private DrawNumber m_DrawNumber;
        private TimeMan m_TimeMan;
        private GameResourceMan m_GameResourceMan;

        private BaseStage m_Stage;

        public SceneMan(BaseStage baseStage)
        {
            m_Stage = baseStage;
            m_GameResourceMan = GameResourceMan.GetResourceMan();
            m_TimeMan = new TimeMan();
        }

        public void SetDrawNumber( DrawNumber drawNumber )
        {
            m_DrawNumber = drawNumber;
        }

        public void AddScene(ICmpScene minimap)
        {
            m_SceneList.Add(minimap);
        }

        public ICmpScene GetScene(int index)
        {
            return m_SceneList[index];
        }

        
        
        public void Release()
        {
            if (m_SceneList == null) return;
            for (int i = m_SceneList.Count - 1; i >= 0; i--)
            {
               ICmpScene cmpScene = m_SceneList[i];
               SceneData sceneData = (SceneData)cmpScene.GetSceneData();
               sceneData.Release();
            }
            
            m_SceneList.Clear();
            m_SceneList = null;
            m_Stage = null;
        }

        private void DrawUnChosenScene(GameTime gameTime, SpriteBatch spriteBatch)
        {          

            //선택되지 않은 씬만 그림.
            foreach (ICmpScene scene in m_SceneList)
            {
                if (!scene.IsSelectedScene())
                {
                    ObjectManager objMan = scene.GetObjectManger();

                    ICmpRender minimapDraw = (ICmpRender)objMan.QueryInterface(scene.GetObjectID(), InterfaceID.IID_RENDER);

                    minimapDraw.Draw(gameTime , spriteBatch);
                }
            }
        }

        private void CheckSceneSelect()
        {
            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();

            TouchCollection collection = input.GetCurrentTouch();

            if (collection.Count > 0)
            {
                foreach (TouchLocation touch in collection)
                {
                    if (touch.State == TouchLocationState.Pressed)
                    {
                        if (IsPressScene(touch) == true)
                            return;
                    }
                }
            }
        }

        private bool IsPressScene( TouchLocation touch )
        {
            foreach (ICmpScene miniE in m_SceneList)
            {
                EntityInfo info = (EntityInfo)miniE.GetEntityInfo();

                if (Collision.ContainPoint(info.mPosition, info.mSize, touch.Position) == true)
                {
                    //클릭되었다.. 상태변경.
                    miniE.SetSelectedScene();
                    return true;
                }
            }
          
            return false;
        }

        public void Update(GameTime gameTime)
        {           
            m_TimeMan.Update(gameTime);
            CheckSceneSelect();

            foreach (ICmpScene cmpScene in m_SceneList)
            {
                SceneData sceneData = (SceneData)cmpScene.GetSceneData();
                IUpdateAble updateAble = sceneData.GetUpdateAble();
                updateAble.SetData(cmpScene.IsSelectedScene());
                updateAble.Update(gameTime);            
            }

            m_Stage.CheckGameState();
        }

        public void Draw(GameTime gameTime , SpriteBatch sb)
        {
            foreach( ICmpScene cmpScene in m_SceneList )
            {
                if (!cmpScene.IsSelectedScene()) continue; // 선택된것만 그리기 위해
                SceneData sceneData = (SceneData)cmpScene.GetSceneData();
                IDrawAble drawAble = sceneData.GetDrawAble();
                drawAble.Draw(gameTime, sb);
            }

            DrawUnChosenScene(gameTime, sb);
            LifeMan.GetLifeMan().Draw(sb);
            m_DrawNumber.Draw(sb, 0, 0, ((int)(m_TimeMan.GetTime())).ToString());
            m_DrawNumber.Draw(sb, 200, 0, ((int)(m_GameResourceMan.GetMoney())).ToString());
        }
    }
}
